package com.Action;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.Bean.wdhtbuildModel;
import com.Bean.wdhtprojectModel;
import com.Bean.wdhtroomModel;
import com.Service.wdhtbuildService;
import com.Service.wdhtprojectService;
import com.Service.wdhtroommodelService;

@Controller
@RequestMapping("/htbuild")
public class wdhdbuildAction {
	@Autowired
    private wdhtbuildService service;
	@Autowired
	private wdhtprojectService wdhtprojectService;
	@Autowired
	private wdhtroommodelService serviceroom;
	@RequestMapping("/listhtbuild.do")
	public String list(Model model){
	  System.out.println("rrrrrbb");
	  List<wdhtbuildModel> selectall = service.selectall();
	  model.addAttribute("list",selectall);
	  return "Wdbuild";
	}
	@RequestMapping("/goaddhtbuild.do")
	public String goadd(Model model){
		List<wdhtprojectModel> selectalls = wdhtprojectService.selectall();
		for(wdhtprojectModel a:selectalls){
			System.out.println(a.getProjectName());
		}
		model.addAttribute("list",selectalls);
		  return "Addhtbuild";
		}
	@RequestMapping("/addhtbuild.do")
	public String add(wdhtbuildModel bean){
		service.insertSelective(bean);		 
		  return "redirect:listhtbuild.do";
		}
	
	@RequestMapping("/deletehtbuild.do")
	public String delete(Model model,wdhtbuildModel a){
		    System.out.println(a.getBuildId());			
			wdhtbuildModel selectByPrimaryId = service.selectByPrimaryId(a.getBuildId());//获得楼栋所有信息		
			wdhtroomModel record =new wdhtroomModel();
			record.setBuildId(selectByPrimaryId.getBuildName());
			record.setProjectId(selectByPrimaryId.getProjectId());
			serviceroom.deleteByPrimaryroom(record);
			service.deleteByPrimaryKey(a.getBuildId());
			  return "redirect:listhtbuild.do";
			}
	
    @RequestMapping("/goupdatehtbuild.do")
	public String goupdate(Model model,wdhtbuildModel a){
    	/*try {
			a.setBuildName(new String(a.getBuildName().getBytes("ISO-8859-1"),"utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
    	System.out.println("a"+a.getBuildName());
    	wdhtbuildModel selectByPrimaryKey = service.selectByPrimaryKey(a.getBuildName());
    	if(selectByPrimaryKey!=null){
    	System.out.println("in goupdate"+selectByPrimaryKey.getBuildName());
    	}else{
    		System.out.println("in goupdate kong!!");
    	}
		 model.addAttribute("bean",selectByPrimaryKey);
		 List<wdhtprojectModel> selectalls = wdhtprojectService.selectall();
		 model.addAttribute("list",selectalls);
		  return "Updatehtbuild";
		}
    @RequestMapping("/updatehtbuild.do")
    public String updatehdproject(wdhtbuildModel bean){
    	service.updateByPrimaryKeySelective(bean);
    	return "redirect:listhtbuild.do";   
    }
    @InitBinder  
	 public void initBinder(WebDataBinder binder) {  
	     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
	     dateFormat.setLenient(false);  
	     binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	 }
}